package com.bpzj.util.pdf;

import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.*;

import java.io.IOException;
import java.util.Iterator;

public class ReadPdfImage {
    public static void main(String[] args) throws Exception {
        PdfReader pdfReader = new PdfReader("didi2.pdf");
//        PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("c:\\out.pdf"));
//        PdfWriter writer = stp.getWriter();
//        Image img = Image.getInstance("image.png");
        PdfDictionary pg = pdfReader.getPageN(1);
        PdfReaderContentParser pdfReaderContentParser = new PdfReaderContentParser(pdfReader);
        pdfReaderContentParser.processContent(1, new RenderListener() {
            @Override
            public void beginTextBlock() {
            }

            @Override
            public void renderText(TextRenderInfo renderInfo) {
                String text = renderInfo.getText();
                if (text != null) {
                    System.out.print(text);
                   // System.out.print(renderInfo.getFont().getFullFontName()[0]);
                    System.out.println(renderInfo.getBaseline().getStartPoint());
                }
            }

            @Override
            public void endTextBlock() {
            }

            @Override
            public void renderImage(ImageRenderInfo renderInfo) {
                try {
                    PdfImageObject image = renderInfo.getImage();
                    if (image != null) {
                       // System.out.println(renderInfo.getStartPoint());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });




        // 另一种解析方法
        PdfDictionary res = (PdfDictionary) PdfReader.getPdfObject(pg.get(PdfName.RESOURCES));
        PdfDictionary xobj = (PdfDictionary) PdfReader.getPdfObject(res.get(PdfName.XOBJECT));
        if (xobj != null) {
            for (Iterator it = xobj.getKeys().iterator(); it.hasNext(); ) {
                PdfObject obj = xobj.get((PdfName) it.next());
                if (obj.isIndirect()) {
                    PdfDictionary tg = (PdfDictionary) PdfReader.getPdfObject(obj);
                    PdfName type = (PdfName) PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE));
                    if (PdfName.IMAGE.equals(type)) {
                        // System.out.println(tg);
//                        PdfReader.killIndirect(obj);
//                        Image maskImage = img.getImageMask();
//                        if (maskImage != null)
//                            writer.addDirectImageSimple(maskImage);
//                        writer.addDirectImageSimple(img, (PRIndirectReference) obj);
                        break;
                    }
                }
            }
        }
    }
}
